$(document).ready(function(){
	//初始化文件输入框
    initFileInput("file_upload", urlcore + "/api/attachment/upload","#goodsUrl");
    //详情说明
	initMySummernote("goodsDetail");
	
 
	var id=getvl("id");
	if(id!=undefined && id!=""){
		loadData(id);
	}
});

//显示数据(修改)
function loadData(id){
		$('#jiu_picture_div').css("display", "block");  
	    $.ajax({  
        type : "GET",  
        url : urlcore + "/api/goods/goodsDetails?goodsId="+id, //图片上传出来的url，返回的是图片上传后的路径，http格式  
        dataType : "json",
        headers:{"Content-Type":"application/json;charset=UTF-8"},
        data : {},
        success: function(data) {//data是返回的hash,key之类的值，key是定义的文件名 
        	var da=data.data[0];
        	if(data.success==true){
        	//处理规格和日期
        	var unitPrices = " ";
            var marketPrices = " ";
            var unitStandards = " ";        
            for (var i = 0; i < data.data.length; i++){
            	unitPrices += data.data[i].unitPrice+",";
            	marketPrices += data.data[i].marketPrice+","; 
            	unitStandards += data.data[i].unitStandard+",";            	 
            }
            marketPrices=(marketPrices.substring(marketPrices.length-1)==',')?marketPrices.substring(0,marketPrices.length-1):marketPrices;
           	unitStandards=(unitStandards.substring(unitStandards.length-1)==',')?unitStandards.substring(0,unitStandards.length-1):unitStandards;
           	unitPrices=(unitPrices.substring(unitPrices.length-1)==',')?unitPrices.substring(0,unitPrices.length-1):unitPrices;
           	
            $("#title").val(da.goods.title);
            $("#unitPrice").val(unitPrices);
        	$("#marketPrice").val(marketPrices);
            $("#unitStandard").val(unitStandards);
        	$("#goodsType").find("option[value='"+da.goods.goodsType+"']").attr("selected",true);//商品类型
        	    var goodsUrl = da.goods.goodsUrl,imgStr1="";
                    if (goodsUrl != null && goodsUrl != '') {
                        goodsUrl= goodsUrl.split("***");
                        $.each(goodsUrl,function(index,value){
                            imgStr1 += 
                            	'<div style="position:relative;width:250px;height:250px;float:left;" display="block">'+
                            	'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
                            	'	</span>'+
                            	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+value+'">'+
                        		'</div>';
                        });
                    }
                    $('#goodsUrl').html(imgStr1);
            $('#goodsDetail').summernote('code',da.goods.goodsDetail);
        	}else {
                alert(data.msg);
            }
        },
        error:function(){  
            alert("失败");  
        }  
    }); 
}











//表达提交
function onSave(){
	var id=getvl("id");
	var title=$("#title").val();
	var goodsType=$("#goodsType").val();
	//活动图片地址
    var goodsUrl = "";
    $("#goodsUrl img").each(function(index,item){
		goodsUrl += $(this).attr("src")+"***";
	});
	if(goodsUrl!=""){
		  goodsUrl = goodsUrl.substring(0, goodsUrl.length-3);
	}
	var goodsDetail=$('#goodsDetail').summernote('code');
	//商品规格
	var unitStandard=$("#unitStandard").val();
	//价格
	var marketPrice=$("#marketPrice").val();
	var unitPrice=$("#unitPrice").val();
	
	
	    $.ajax({  
        type : "POST",  
        url : urlcore + "/api/goodsUnit/addOrUpdateGoodsUnit?marketPrice="+marketPrice+"&unitPrice="+unitPrice+"&id="+id, //图片上传出来的url，返回的是图片上传后的路径，http格式  
        dataType : "json",
        headers:{"Content-Type":"application/json;charset=UTF-8"},
        data :  JSON.stringify({
        	unitStandard:unitStandard,
    		status:1,
        	goods:{
        			title:title,
		        	goodsType:goodsType,
		        	goodsUrl:goodsUrl,
		        	goodsDetail:goodsDetail,	  
		        }       		
        	}),
        success: function(data) {//data是返回的hash,key之类的值，key是定义的文件名 
        	if(data.success==true){
        		if (id=="") {
                    if (confirm("添加成功，是否继续添加？")) {
                        //刷新
                        location.reload();
                    } else {
                        //跳转列表页
                        window.location.href = "goods_list.html?goodsType="+goodsType;
                    }
                }else{
                    alert("修改成功");
                    //跳转列表页
                        window.location.href = "goods_list.html?goodsType="+goodsType;
                }
        	}else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error:function(){  
            alert("上传失败");  
        }  
    }); 
}
